﻿using AutoMapper;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using Traceability.APP.Application.Command.Traceablitys;
using Traceability.APP.DTO;
using Traceability.Damoin;
using Traceability.ErrorCode;
using Traceability.Infaseture;

namespace Traceability.APP.Application.CommandHadnLer.Traceablitys
{
    public class GetTraceablityCommandHandLer : IRequestHandler<GetTraceablityCommand, APIResult<APIPaing<List<TraceablityDto>>>>
    {

        private readonly IBaseRepostiy<Traceablity> _traceablity;
        private readonly IBaseRepostiy<Goods> _goods;
        private readonly IMapper mapper;

        public GetTraceablityCommandHandLer(IBaseRepostiy<Traceablity> traceablity, IBaseRepostiy<Goods> goods, IMapper mapper)
        {
            _traceablity = traceablity;
            _goods = goods;
            this.mapper = mapper;
        }

        public async Task<APIResult<APIPaing<List<TraceablityDto>>>> Handle(GetTraceablityCommand request, CancellationToken cancellationToken)
        {
            // 异步获取所有Traceablity数据
            var users = await Task.Run(() => _traceablity.GetAll().ToList());
            var userdtos = new List<TraceablityDto>();
            foreach (var user in users)
            {
                var goods = _goods.GetAll().FirstOrDefault(x => x.SId == user.SId);
                string goodsName = goods != null ? goods.GoodsName : string.Empty;
                userdtos.Add(new TraceablityDto
                {
                    Id = user.Id,
                    SId = user.SId,
                    Batch = user.Batch,
                    ShelfLife = user.ShelfLife,
                    LicenseNumber = user.LicenseNumber,
                    Enterprise = user.Enterprise,
                    Percentage = user.Percentage,
                    Url = user.Url,
                    CreateUserTime = user.CreateUserTime,
                    CreateUserName = user.CreateUserName,
                    UpdateUserTime = user.UpdateUserTime,
                    UpdateUserName = user.UpdateUserName,
                    DeleteUserName = user.DeleteUserName,
                    DeleteUserTime = user.DeleteUserTime,
                    IsDeleted = user.IsDeleted,
                    GoodsName = goodsName
                });
            }

            var list = userdtos.Where(x => !x.IsDeleted).ToList();
            if (request.SId != null)
            {
                list = list.Where(x => x.SId == request.SId).ToList();
            }
            if (!string.IsNullOrEmpty(request.LicenseNumber))
            {
                list = list.Where(x => x.LicenseNumber == request.LicenseNumber).ToList();
            }
            if (!string.IsNullOrEmpty(request.Batch))
            {
                list = list.Where(x => x.Batch == request.Batch).ToList();
            }
            if (request.ShelfLife != null)
            {
                list = list.Where(x => x.ShelfLife == request.ShelfLife).ToList();
            }
            if (!string.IsNullOrEmpty(request.Enterprise))
            {
                list = list.Where(x => x.Enterprise == request.Enterprise).ToList();
            }
            int totalCount = list.Count();
            int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            // 分页
            list = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return new APIResult<APIPaing<List<TraceablityDto>>>
            {
                Code = ResultCode.Ok,
                Data = new APIPaing<List<TraceablityDto>>
                {
                    Data = list,
                    totalCount = totalCount,
                    totalPage = totalPage,
                },
                Message = "获取成功"
            };
        }
    }
}
